package libase.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import nullutil.NullUtil;

/**
 *
 * @author aspr0
 */
@Entity
@Table (name = "AUTHOR")
@XmlRootElement
@NamedQueries (
        {
    @NamedQuery (name = "Author.findAll", query = "SELECT a FROM Author a ORDER BY a.id"),
    @NamedQuery (name = "Author.findById", query = "SELECT a FROM Author a WHERE a.id = :id"),
    @NamedQuery (name = "Author.findByLastNm", query = "SELECT a FROM Author a WHERE a.lastNm = :lastNm"),
    @NamedQuery (name = "Author.findByFirstNm", query = "SELECT a FROM Author a WHERE a.firstNm = :firstNm"),
    @NamedQuery (name = "Author.findByMiddleNm", query = "SELECT a FROM Author a WHERE a.middleNm = :middleNm")
})
public class Author implements Serializable
{
    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @SequenceGenerator (allocationSize = 1, name = "sag", sequenceName = "SEQ_AUTHOR")
    @GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "sag")
    @Id
    @Basic (optional = false)
    @NotNull
    @Column (name = "ID")
    private BigDecimal id;
    @Basic (optional = false)
    @NotNull
    @Size (min = 1, max = 200)
    @Column (name = "LAST_NM")
    private String lastNm;
    @Basic (optional = false)
    @NotNull
    @Size (min = 1, max = 200)
    @Column (name = "FIRST_NM")
    private String firstNm;
    @Size (max = 200)
    @Column (name = "MIDDLE_NM")
    private String middleNm;
    @OneToMany (cascade = CascadeType.ALL, mappedBy = "auth")
    private Collection<Book> bookCollection;

    public Author()
    {
    }

    public Author(BigDecimal id)
    {
        this.id = id;
    }

    public Author(BigDecimal id, String lastNm, String firstNm)
    {
        this.id = id;
        this.lastNm = lastNm;
        this.firstNm = firstNm;
    }

    public BigDecimal getId()
    {
        return id;
    }

    public void setId(BigDecimal id)
    {
        this.id = id;
    }

    public String getLastNm()
    {
        return lastNm;
    }

    public void setLastNm(String lastNm)
    {
        this.lastNm = lastNm;
    }

    public String getFirstNm()
    {
        return firstNm;
    }

    public void setFirstNm(String firstNm)
    {
        this.firstNm = firstNm;
    }

    public String getMiddleNm()
    {
        return middleNm;
    }

    public void setMiddleNm(String middleNm)
    {
        this.middleNm = middleNm;
    }

    @XmlTransient
    public Collection<Book> getBookCollection()
    {
        return bookCollection;
    }

    public void setBookCollection(Collection<Book> bookCollection)
    {
        this.bookCollection = bookCollection;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Author))
        {
            return false;
        }
        Author other = (Author) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return String.format("%s %s %s", lastNm, firstNm, NullUtil.toString(middleNm));
    }
}
